require(['jquery',
         'global',
         'module/util',
         'module/ajax',
         'jquery-weui',
         'jquery.validate',
         'bootstrap',
         'requirejs/domready!'], 
function($, global, util, bootbox){
	
	(function() {
		var content = $("#formPanel");
		var height = $(document.body).height(), width = $(document.body).width();
		var contentHeight = content.height(), contentWidth = content.width();
		$("#bodyPanel").css({
			height : height,
			'padding-top' : ((height  - contentHeight) / 2),
			'padding-left' : ((width-contentWidth) / 2)
		});
		content.show();
	})();
	
	$(".weui_switch input[type='checkbox']").change(function() {
		if($(this).prop("checked")) {
			$(this).val(3);
		} else {
			$(this).val(2);
		}
	});
	
	$("#cancelBtn").click(function() {
		util.redirect("/");
	});

	var doRegist = function(dto) {
		var $button = $(".submit");
		$.ajax({
           url : global.context + '/web/weixin/usermanage/do-regist',
           data : JSON.stringify(dto),
           type : 'POST',
           dataType : 'json',
           contentType : 'application/json;charset=utf-8',
           success : function(result) {
        	   if(result && result.code == "ACK") {
        		   $.ajax({
        	           url : global.context + "/web/auth/login/authc",
        	           data : JSON.stringify(dto),
        	           headers : {'x-form-id' : "signin-form" },
        	           type : 'POST',
        	           dataType : 'json',
        	           contentType : 'application/json;charset=utf-8'
        		   });
        	   }
           },
           beforeSend : function(){
        	   $button.val("正在注册...").attr("disabled", true);
           },
	       complete : function(XMLHttpRequest,status){ //请求完成后最终执行参数
	    	   $button.val("提交注册").attr("disabled", false);
	       }
        });	
	}
	
	// Add event handler for captcha image click
	/*$("#captcha-img").click(function() {
	    $(this).attr("src", global.context+ "/web/auth/captcha?r=" + Math.random());
	});*/
	
	// Add jQuery validate to sign-in form
	$('#signin-form').validate({
		onkeyup : false,
		rules:{
			username:{
				required: true,
				rangelength: [11,11]
            },
            password:{
				required: true,
				rangelength: [8,30]
		    },
		    confirmPassword: {
				required: true,
				rangelength: [8,30],
		        equalTo : '#password'
		    }
		    /*,captcha:{
				required: true,
				minlength:[5], 
				maxlength: [5]
		    }*/
	    },
	    messages : {
	    	username:{
	    		 required: "用户名不能为空",
	    		 rangelength : $.format("用户名长度：{0}-{1}个字符")
	        },
	        password:{
		  		required: "密码不能为空",
		  		rangelength : $.format("密码长度：{0}-{1}个字符")
		    },
		    confirmPassword:{
		  		required: "确认密码不能为空",
		  		rangelength : $.format("确认密码长度：{0}-{1}个字符")
		    }
	        /*,captcha:{
		  		required: "验证码不能为空",
		  		rangelength : $.format("验证码为5个字符")
		    }*/
	    },submitHandler: function(form) {
	    	var dto = {};
	    	$(form).find('input,textarea,select').each(function() {
	    		if(this.name && this.value) {
		    		dto[this.name] = this.value;
	    		}
	    	});
	    	doRegist(dto);
			//util.ajax_submit(form).complete(function(){
				//$('#captcha-img').attr("src", global.context+ "/web/auth/captcha?r=" + Math.random());
			//});
	    },
	    highlight: function(element, errorClass, validClass) {
	    	var parent = $(element).parent().parent();
	    	$(element).addClass(errorClass).removeClass(validClass);
	    	parent.addClass('weui_cell_warn').removeClass('has-success').addClass('has-error').find('.fa').remove();
	    	parent.append('<i class="fa fa-exclamation-circle form-control-feedback"></i>');
	    },
	    unhighlight: function(element, errorClass, validClass) {
	    	var parent = $(element).parent().parent();
	    	$(element).removeClass(errorClass).addClass(validClass);
	    	parent.removeClass('weui_cell_warn').removeClass('has-error').addClass('has-success').find('.fa').remove();
	    	parent.append('<i class="fa fa-check form-control-feedback"></i>');
	    }
	});
	
});
	